package com.study.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.study.entity.InfoCount;
import com.study.entity.PageBean;
import com.study.entity.Student;
import com.study.entity.StudentQueryParam;
import com.study.mapper.StudentMapper;
import com.study.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean list(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        Page list = (Page) studentMapper.list(param);
        long total = list.getTotal();
        List result = list.getResult();
        PageBean pb = new PageBean(total, result);
        return pb;
    }

    @Override
    public void delete(Integer id) {
        studentMapper.delete(id);
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    @Override
    public Student getByid(Integer id) {
        Student student = studentMapper.getById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public List<Map<String, Object>> degreeCount() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<InfoCount> counts = studentMapper.degreeCount();
        for (InfoCount count : counts) {
            Map<String, Object> map = new LinkedHashMap<>();
            switch ((Integer) count.getInfo()) {
                case 1:
                    count.setInfo("初中");
                    break;
                case 2:
                    count.setInfo("高中");
                    break;
                case 3:
                    count.setInfo("大专");
                    break;
                case 4:
                    count.setInfo("本科");
                    break;
                case 5:
                    count.setInfo("硕士");
                    break;
                case 6:
                    count.setInfo("博士");
                    break;
            }
            map.put("name", count.getInfo());
            map.put("value", count.getCount());
            list.add(map);
        }
        return list;
    }


}
